<template>
<div>

  <div style="padding: 10px 0">
    <el-input placeholder="请输入订单状态" style="width: 200px;" suffix-icon="el-icon-search" v-model="state"></el-input>
    <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
    <el-button  type="warning" @click="reset">重置</el-button>

  </div>
  <el-table :data="tableData" border stripe     @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55">
    </el-table-column>
    <el-table-column prop="orderId" label="订单号"  width="170">
    </el-table-column>
    <el-table-column prop="goodscount" label="斤数" >
    </el-table-column>
    <el-table-column prop="totalprice" label="总价">
    </el-table-column>
    <el-table-column prop="name" label="垃圾名">
    </el-table-column>
    <el-table-column prop="image" label="垃圾图片">
      <template slot-scope="scope">
        <el-image style="width: 70px; height: 50px" :src="scope.row.image" :preview-src-list="[scope.row.image]">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
      </template>
    </el-table-column>
    <el-table-column prop="address" label="地址">
    </el-table-column>
    <el-table-column prop="phone" label="手机号">
    </el-table-column>
    <el-table-column prop="state" label="状态">
      <template slot-scope="scope">
      <el-tag size="15"  :type="scope.row.state==='已接单'?'':'warning'">{{scope.row.state}}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="操作" >
      <template slot-scope="scope">
        <el-button type="primary" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i> </el-button>
      </template>
    </el-table-column>
  </el-table>
  <div style="padding: 10px 0">

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
  <el-dialog title="订单信息" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="80px" size="small">
      <el-form-item label="状态" >
        <el-dropdown  @command="handleCommand">
       <span class="el-dropdown-link">
             {{form.state}}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
          <el-dropdown-menu  slot="dropdown">
            <el-dropdown-item command="已接单">已接单</el-dropdown-item>
            <el-dropdown-item command="未接单">未接单</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "order",
  data(){

    return {
      tableData:[],
      total:0,
      pageNum:1,
      pageSize:10,
      goodscount:"",
      totalprice:"",
      address:"",
      phone:"",
      state:"",
      dialogFormVisible:false,
      form:{},
      multipleSelection:[]

    }
  },
  created() {
    this.load()
  },
  methods:{
    load(){
      this.request.get("/ljhsorder/page",{
        params:{
          pageNum:this.pageNum,
          pageSize:this.pageSize,
          state:this.state
        }
      }).then(res=>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    handleSelectionChange(val){
      this.multipleSelection=val
    },
    save(){
      if(this.form.state=="未接单"){
        this.form.collector_id=null
      }else if(this.form.state=="已接单"){
        this.form.collector_id=1
      }
      this.request.post("/ljhsorder",this.form).then(res=>{
        if(res){
          this.$message.success("保存成功")
          this.dialogFormVisible=false
          this.load()
        }else{
          this.$message.error("保存失败")
        }
      })
    },
    reset(){
      this.state=""
      this.load()
    },
    del(orderId){
      this.request.delete("/ljhsorder/"+orderId).then(res=>{
        if (res) {
          this.$message.success("删除成功")
          this.load()
        }else {
          this.$message.error("删除失败")
        }
      })
    },
    delBatch(){
      let ids=this.multipleSelection.map(v=>v.orderId)
      this.request.post("/ljhsorder/batch/del",ids).then(res=>{
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        }else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleEdit(row){
      this.form=row
      this.dialogFormVisible=true
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.pageNum=pageNum
      this.load()
    },
    handleCommand(command){
      this.form.state=command
    }
  }
}
</script>

<style scoped>

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>